home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / SDKs / Apple Game Sprockets / SoundSprocket / SoundSprocketTest Sources / TS3Utils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-26  |  11.1 KB  |  468 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    File:        TS3Utils.c
  3.  *
  4.  *    Contents:    Some utilities.
  5.  *
  6.  *    Copyright © 1996 Apple Computer, Inc.
  7.  */
  8.  
  9. #include <assert.h>
  10. #include <math.h>
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #include "TS3Utils.h"
  15.  
  16.  
  17. #ifndef HONOR_RANGE
  18.     #define HONOR_RANGE        0
  19. #endif
  20.  
  21.  
  22. static UserItemUPP            gUtilsOKUserItemProc    = NULL;
  23.  
  24.  
  25. static pascal void Utils_OKUserItem(
  26.     DialogPtr            inDialog,
  27.     short                inItem);
  28.  
  29.  
  30. /* =============================================================================
  31.  *        Utils_Init (external)
  32.  *
  33.  *    Initializes the utilities.
  34.  * ========================================================================== */
  35. void Utils_Init(
  36.     void)
  37. {
  38. }
  39.  
  40.  
  41. /* =============================================================================
  42.  *        Utils_Exit (external)
  43.  *
  44.  *    Prepares for exit.
  45.  * ========================================================================== */
  46. void Utils_Exit(
  47.     void)
  48. {
  49.     if (gUtilsOKUserItemProc != NULL)
  50.     {
  51.         DisposeRoutineDescriptor(gUtilsOKUserItemProc);
  52.         gUtilsOKUserItemProc = NULL;
  53.     }
  54. }
  55.  
  56.  
  57. /* =============================================================================
  58.  *        Utils_Interval (external)
  59.  *
  60.  *    Returns the interval, in seconds, between two results of Microseconds.
  61.  * ========================================================================== */
  62. float Utils_Interval(
  63.     const UnsignedWide*    inPrevTime,
  64.     const UnsignedWide*    inCurrTime)
  65. {
  66.     //• Should actually take high words into consideration too
  67.     return 0.000001*(inCurrTime->lo - inPrevTime->lo);
  68. }
  69.  
  70.  
  71. /* =============================================================================
  72.  *        Utils_OKUserItem (internal)
  73.  *
  74.  *    Draws the user item used for framing the OK button.  The user item should
  75.  *    be four pixels larger than the OK button in each direction.
  76.  * ========================================================================== */
  77. pascal void Utils_OKUserItem(
  78.     DialogPtr            inDialog,
  79.     short                inItem)
  80. {
  81.     short                itemType;
  82.     Handle                itemHandle;
  83.     Rect                itemBounds;
  84.     
  85.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  86.     
  87.     PenSize(3, 3);
  88.     FrameRoundRect(&itemBounds, 16, 16);
  89.     PenSize(1, 1);
  90. }
  91.  
  92.  
  93. /* =============================================================================
  94.  *        Utils_GetOKUserItemProc (external)
  95.  *
  96.  *    Returns the UPP for the dialog user item that outlines the default button.
  97.  * ========================================================================== */
  98. UserItemUPP Utils_GetOKUserItemProc(
  99.     void)
  100. {
  101.     if (gUtilsOKUserItemProc == NULL)
  102.     {
  103.         gUtilsOKUserItemProc = NewUserItemProc(Utils_OKUserItem);
  104.         assert(gUtilsOKUserItemProc != NULL);
  105.     }
  106.     
  107.     return gUtilsOKUserItemProc;
  108. }
  109.  
  110.  
  111. /* =============================================================================
  112.  *        Utils_SetStr255Field (external)
  113.  *
  114.  *    Changes the text of the given dialog item to the given string value.  If
  115.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  116.  *    of course be editable or static text.
  117.  * ========================================================================== */
  118. void Utils_SetStr255Field(
  119.     DialogPtr            inDialog,
  120.     short                inItem,
  121.     ConstStr255Param    inValue,
  122.     Boolean                inHasValue)
  123. {
  124.     short                itemType;
  125.     Handle                itemHandle;
  126.     Rect                itemBounds;
  127.     
  128.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  129.     if (inHasValue)
  130.     {
  131.         SetDialogItemText(itemHandle, inValue);
  132.     }
  133.     else
  134.     {
  135.         SetDialogItemText(itemHandle, "\p");
  136.     }
  137. }
  138.  
  139.  
  140. /* =============================================================================
  141.  *        Utils_GetStr255Field (external)
  142.  *
  143.  *    Interprets the value of the given editable or static text field as an
  144.  *    string value.  If outHasValue is NULL then the field must not be blank.
  145.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  146.  *    and true if it has a value.  Returns false if there is a formatting problem.
  147.  * ========================================================================== */
  148. Boolean Utils_GetStr255Field(
  149.     DialogPtr            inDialog,
  150.     short                inItem,
  151.     Str255                outValue,
  152.     Boolean*            outHasValue)
  153. {
  154.     short                itemType;
  155.     Handle                itemHandle;
  156.     Rect                itemBounds;
  157.     
  158.     // Grab the string
  159.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  160.     GetDialogItemText(itemHandle, outValue);
  161.     
  162.     if (outHasValue != NULL)
  163.     {
  164.         *outHasValue = outValue[0] > 0;
  165.     }
  166.     
  167.     return true;
  168. }
  169.  
  170.  
  171. /* =============================================================================
  172.  *        Utils_SetUInt32Field (external)
  173.  *
  174.  *    Changes the text of the given dialog item to the given integer value.  If
  175.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  176.  *    of course be editable or static text.
  177.  * ========================================================================== */
  178. void Utils_SetUInt32Field(
  179.     DialogPtr            inDialog,
  180.     short                inItem,
  181.     UInt32                inValue,
  182.     Boolean                inHasValue)
  183. {
  184.     Str255                str;
  185.     short                itemType;
  186.     Handle                itemHandle;
  187.     Rect                itemBounds;
  188.     
  189.     if (inHasValue)
  190.     {
  191.         sprintf((char*) str, "x%lu", inValue);
  192.         str[0] = strlen((char*) str) - 1;
  193.     }
  194.     else
  195.     {
  196.         str[0] = 0;
  197.     }
  198.     
  199.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  200.     SetDialogItemText(itemHandle, str);
  201. }
  202.  
  203.  
  204. /* =============================================================================
  205.  *        Utils_GetUInt32Field (external)
  206.  *
  207.  *    Interprets the value of the given editable or static text field as an
  208.  *    integer value.  If outHasValue is NULL then the field must not be blank.
  209.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  210.  *    and true if it has a value.  Returns false if there is a formatting problem.
  211.  * ========================================================================== */
  212. Boolean Utils_GetUInt32Field(
  213.     DialogPtr            inDialog,
  214.     short                inItem,
  215.     UInt32*                outValue,
  216.     Boolean*            outHasValue,
  217.     UInt32                inMin,
  218.     UInt32                inMax)
  219. {
  220.     Str255                str;
  221.     short                itemType;
  222.     Handle                itemHandle;
  223.     Rect                itemBounds;
  224.     
  225.     // Grab the string
  226.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  227.     GetDialogItemText(itemHandle, str);
  228.     str[str[0]+1] = 0;
  229.     
  230.     // Parse it
  231.     if (sscanf((char*) str, "%*c%lu", outValue) > 0)
  232.     {
  233.         // Got a number
  234.         if (outHasValue != NULL)
  235.         {
  236.             *outHasValue = true;
  237.         }
  238.         
  239.         // Check the range
  240.         #if HONOR_RANGE
  241.             if (*outValue < inMin || *outValue > inMax)
  242.             {
  243.                 return false;
  244.             }
  245.         #endif
  246.     }
  247.     else
  248.     {
  249.         if (outHasValue != NULL)
  250.         {
  251.             *outHasValue = false;
  252.         }
  253.         else
  254.         {
  255.             return false;
  256.         }
  257.     }
  258.     
  259.     return true;
  260. }
  261.  
  262.  
  263. /* =============================================================================
  264.  *        Utils_SetFloatField (external)
  265.  *
  266.  *    Changes the text of the given dialog item to the given float value.  If
  267.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  268.  *    of course be editable or static text.
  269.  * ========================================================================== */
  270. void Utils_SetFloatField(
  271.     DialogPtr            inDialog,
  272.     short                inItem,
  273.     float                inValue,
  274.     Boolean                inHasValue)
  275. {
  276.     Str255                str;
  277.     short                itemType;
  278.     Handle                itemHandle;
  279.     Rect                itemBounds;
  280.     float                abs;
  281.     float                fract;
  282.     
  283.     if (inHasValue)
  284.     {
  285.         abs = fabsf(inValue);
  286.         fract = abs-floorf(abs);
  287.         if (fract < 0.0001 || fract > 0.9999)
  288.         {
  289.             // Value is close enough to integer to show it that way
  290.             sprintf((char*) str, "x%.0f", inValue);
  291.         }
  292.         else
  293.         {
  294.             // Has a fractional part
  295.             sprintf((char*) str, "x%.2f", inValue);
  296.         }
  297.         str[0] = strlen((char*) str) - 1;
  298.     }
  299.     else
  300.     {
  301.         str[0] = 0;
  302.     }
  303.     
  304.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  305.     SetDialogItemText(itemHandle, str);
  306. }
  307.  
  308.  
  309. /* =============================================================================
  310.  *        Utils_GetFloatField (external)
  311.  *
  312.  *    Interprets the value of the given editable or static text field as an
  313.  *    float value.  If outHasValue is NULL then the field must not be blank.
  314.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  315.  *    and true if it has a value.  Returns false if there is a formatting problem.
  316.  * ========================================================================== */
  317. Boolean Utils_GetFloatField(
  318.     DialogPtr            inDialog,
  319.     short                inItem,
  320.     float*                outValue,
  321.     Boolean*            outHasValue,
  322.     float                inMin,
  323.     float                inMax)
  324. {
  325.     Str255                str;
  326.     short                itemType;
  327.     Handle                itemHandle;
  328.     Rect                itemBounds;
  329.     
  330.     // Grab the string
  331.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  332.     GetDialogItemText(itemHandle, str);
  333.     str[str[0]+1] = 0;
  334.     
  335.     // Parse it
  336.     if (sscanf((char*) str, "%*c%f", outValue) > 0)
  337.     {
  338.         // Got a number
  339.         if (outHasValue != NULL)
  340.         {
  341.             *outHasValue = true;
  342.         }
  343.         
  344.         // Check the range
  345.         #if HONOR_RANGE
  346.             if (*outValue < inMin || *outValue > inMax)
  347.             {
  348.                 return false;
  349.             }
  350.         #endif
  351.     }
  352.     else
  353.     {
  354.         if (outHasValue != NULL)
  355.         {
  356.             *outHasValue = false;
  357.         }
  358.         else
  359.         {
  360.             return false;
  361.         }
  362.     }
  363.     
  364.     return true;
  365. }
  366.  
  367.  
  368. /* =============================================================================
  369.  *        Utils_SetVector3DField (external)
  370.  *
  371.  *    Changes the text of the given dialog item to the given TQ3ector3D value.  If
  372.  *    inHasValue is false, then the field is emptied instead.  The dialog item must
  373.  *    of course be editable or static text.
  374.  * ========================================================================== */
  375. void Utils_SetVector3DField(
  376.     DialogPtr            inDialog,
  377.     short                inItem,
  378.     const TQ3Vector3D*    inValue,
  379.     Boolean                inHasValue)
  380. {
  381.     Str255                str;
  382.     short                itemType;
  383.     Handle                itemHandle;
  384.     Rect                itemBounds;
  385.     float                abs;
  386.     float                fract;
  387.     UInt32                xPrecision;
  388.     UInt32                yPrecision;
  389.     UInt32                zPrecision;
  390.     
  391.     if (inHasValue)
  392.     {
  393.         abs = fabsf(inValue->x);
  394.         fract = abs-floorf(abs);
  395.         xPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  396.         
  397.         abs = fabsf(inValue->y);
  398.         fract = abs-floorf(abs);
  399.         yPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  400.         
  401.         abs = fabsf(inValue->z);
  402.         fract = abs-floorf(abs);
  403.         zPrecision = (fract < 0.0001 || fract > 0.9999) ? 0 : 2;
  404.         
  405.         sprintf((char*) str, "x%.*f %.*f %.*f", xPrecision, inValue->x, yPrecision, inValue->y, zPrecision, inValue->z);
  406.         str[0] = strlen((char*) str) - 1;
  407.     }
  408.     else
  409.     {
  410.         str[0] = 0;
  411.     }
  412.     
  413.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  414.     SetDialogItemText(itemHandle, str);
  415. }
  416.  
  417.  
  418. /* =============================================================================
  419.  *        Utils_GetVector3DField (external)
  420.  *
  421.  *    Interprets the value of the given editable or static text field as an
  422.  *    TQ3Vector3D value.  If outHasValue is NULL then the field must not be blank.
  423.  *    If outHasValue is non-NULL then it is set to false if the field is blank
  424.  *    and true if it has a value.  Returns false if there is a formatting problem.
  425.  * ========================================================================== */
  426. Boolean Utils_GetVector3DField(
  427.     DialogPtr            inDialog,
  428.     short                inItem,
  429.     TQ3Vector3D*        outValue,
  430.     Boolean*            outHasValue)
  431. {
  432.     Str255                str;
  433.     short                itemType;
  434.     Handle                itemHandle;
  435.     Rect                itemBounds;
  436.     
  437.     // Grab the string
  438.     GetDialogItem(inDialog, inItem, &itemType, &itemHandle, &itemBounds);
  439.     GetDialogItemText(itemHandle, str);
  440.     str[str[0]+1] = 0;
  441.     
  442.     // Parse it
  443.     if (sscanf((char*) str, "%*c%f%f%f", &outValue->x, &outValue->y, &outValue->z) == 3)
  444.     {
  445.         // Got a number
  446.         if (outHasValue != NULL)
  447.         {
  448.             *outHasValue = true;
  449.         }
  450.     }
  451.     else
  452.     {
  453.         if (outHasValue != NULL)
  454.         {
  455.             *outHasValue = false;
  456.         }
  457.         else
  458.         {
  459.             return false;
  460.         }
  461.     }
  462.     
  463.     return true;
  464. }
  465.  
  466.  
  467.  
  468.